﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hbse.Entities;
using NHibernate;
using hbse.DTO;

namespace hbse.Service
{
    public class UntilService
    {

        // u1
        public List<hbse.DTO.ProductDTO> getProductsByCode(string _productCode, ISession session)
        {

            List<ProductDTO> productDTOs = new List<ProductDTO>();
            ProductDTO productDTO = null;

            using (session.BeginTransaction())
            {

                var products = session.CreateQuery("from Product p where p.ProductCode like '%" + _productCode + "%' and p.Status='1'").Future<Product>();
                foreach (Product p in products)
                {
                    productDTO = new ProductDTO();
                    productDTO.Id = p.Id;
                    productDTO.ProductCode = p.ProductCode;
                    productDTO.CustomerCode = p.Customer.CustomerCode;
                    productDTOs.Add(productDTO);
                }

            }

            return productDTOs;
        }
        // u2
        public hbse.DTO.ProductDTO getProductByCode(string _productCode, ISession session)
        {

            Product product = new Product();
            ProductDTO productDTO = null;

            using (session.BeginTransaction())
            {

                var products = session.CreateQuery("from Product p where p.ProductCode='" + _productCode + "' and p.Status='1'").Future<Product>();
                product = products.First();    
                productDTO = new ProductDTO();
                productDTO.Id = product.Id;
                productDTO.ProductCode = product.ProductCode;
                productDTO.CustomerCode = product.Customer.CustomerCode;

            }

            return productDTO;
        }
        // u3
        public List<hbse.DTO.ProductDTO> getProductsByCodeTo(string _productCode, ISession session)
        {

            List<ProductDTO> productDTOs = new List<ProductDTO>();
            ProductDTO productDTO = null;

            using (session.BeginTransaction())
            {

                var products = session.CreateQuery("from Product p where p.ProductCode='" + _productCode + "'and ProductType='0' and p.Status='1'").Future<Product>();
                foreach (Product p in products)
                {
                    productDTO = new ProductDTO();
                    productDTO.Id = p.Id;
                    productDTO.ProductCode = p.ProductCode;
                    productDTO.CustomerCode = p.Customer.CustomerCode;
                    productDTOs.Add(productDTO);
                }

            }

            return productDTOs;
        }
        // u4
        public List<hbse.DTO.CustomerDTO> getCustomersAll(ISession session)
        {

            List<CustomerDTO> customerDTOs = new List<CustomerDTO>();
            CustomerDTO customerDTO = null;

            using (session.BeginTransaction())
            {

                var customers = session.CreateQuery("from Customer ").Future<Customer>();
                foreach (Customer c in customers)
                {
                    customerDTO = new CustomerDTO();
                    customerDTO.Id = c.Id;
                    customerDTO.customerCode = c.CustomerCode;
                    customerDTO.customerName = c.CustomerName;
                    customerDTOs.Add(customerDTO);
                }

            }

            return customerDTOs;
        }
        // u5
        public hbse.DTO.CustomerDTO getCustomerDTObyCode(string _customerCode,ISession session)
        {

            Customer customer = new Customer();
            CustomerDTO customerDTO = null;

            using (session.BeginTransaction())
            {
                var customers = session.CreateQuery("from Customer c where c.CustomerCode='"+_customerCode+"' ").Future<Customer>();
                customer = customers.First();
                customerDTO = new CustomerDTO();
                customerDTO.Id = customer.Id;
                customerDTO.customerCode = customer.CustomerCode;
                customerDTO.customerName = customer.CustomerName;
               
            }
            return customerDTO;
        }
        // u6
        public Users getUserbyCode(string _userCode, ISession session)
        {
            Users user = null;
            using (session.BeginTransaction())
            {
                var users = session.CreateQuery("from Users u where u.UserCode='" + _userCode + "' ").Future<Users>();
                user = users.First();
                 

            }
            return user;
        }
        // u7
        public hbse.DTO.OrderDTO getOrderBySaleOrder(string _saleorder, ISession session)
        {

            OrderDTO orderDTO = null;
            using (session.BeginTransaction())
            {
                var orders = session.CreateQuery("from Orders o where o.SaleOrder='" + _saleorder + "' ").Future<Orders>();
                foreach (Orders o in orders)
                {
                    orderDTO = new OrderDTO();
                    orderDTO.Id = o.Id;
                    orderDTO.SaleOrder = o.SaleOrder;
                    orderDTO.ProductCode = o.Product.ProductCode;
                    orderDTO.MrpNo = o.MrpNo;
                    orderDTO.QtyOrder = o.QtyOrder;
                    orderDTO.FGStock = o.FgStock;
                    orderDTO.Qty = o.QtyProduction;
                    orderDTO.Price = o.Price;
                    orderDTO.Barcode = o.Barcode;

                }
            }
            return orderDTO;
        }
        // u8
        public List<hbse.DTO.OrderDTOS> getOrdersBySaleOrder(string _saleorder,ISession session)
        {

            List<OrderDTOS> orderDTOs = new List<OrderDTOS>();
            OrderDTOS orderDTO = null;

            using (session.BeginTransaction())
            {

                var orders = session.CreateQuery("from Orders o where o.SaleOrder='"+ _saleorder+"' ").Future<Orders>();
                foreach (Orders o in orders)
                {
                    orderDTO = new OrderDTOS();
                    orderDTO.Id = o.Id;
                    orderDTO.SaleOrder = o.SaleOrder;
                    orderDTO.ProductCode = o.Product.ProductCode;
                    orderDTOs.Add(orderDTO);
                }

            }

            return orderDTOs;
        }
        // u9
        public List<hbse.DTO.ProductDTO> getProductsByCodeT1(string _productCode, ISession session)
        {

            List<ProductDTO> productDTOs = new List<ProductDTO>();
            ProductDTO productDTO = null;

            using (session.BeginTransaction())
            {

                var products = session.CreateQuery("from Product p where p.ProductCode like '%" + _productCode + "%'and ProductType='1' and p.Status='1'").Future<Product>();
                foreach (Product p in products)
                {
                    productDTO = new ProductDTO();
                    productDTO.Id = p.Id;
                    productDTO.ProductCode = p.ProductCode;
                    productDTO.CustomerCode = p.Customer.CustomerCode;
                    productDTOs.Add(productDTO);
                }

            }

            return productDTOs;
        }
        // u10
        public List<Product> getProductsByCodepType(string _productTypeCode, ISession session)
        {

            List<Product> products = new List<Product>();
            Product product = null;

            using (session.BeginTransaction())
            {

                var vproducts = session.CreateQuery("from Product p where p.ProductType='" + _productTypeCode + "'").Future<Product>();
                products = vproducts.ToList();
                
            }
            return products;
        }
       // u11
        public Customer getCustomerbyCode(string _customerCode, ISession session)
        {
            Customer customer = null;
            using (session.BeginTransaction())
            {
                var customers = session.CreateQuery("from Customer c where c.CustomerCode='" + _customerCode + "' ").Future<Customer>();
                customer = customers.First();
                
            }
            return customer;
        }

        // u12
        public Orders getOrderNotDTOBySaleOrder(string _saleorder, ISession session)
        {

            Orders order  = null;
            using (session.BeginTransaction())
            {
                var orders = session.CreateQuery("from Orders o where o.SaleOrder='" + _saleorder + "' ").Future<Orders>();
                order = orders.First();
                
               
            }
            return order;
        }

        // u13
        public List<hbse.DTO.ProductSamMDTO> getProductsSamMDTOByCodepType(string _productTypeCode, ISession session)
        {

            List<hbse.DTO.ProductSamMDTO> productSamMDTOs = new List<hbse.DTO.ProductSamMDTO>();
            hbse.DTO.ProductSamMDTO productSamMDTO = null;

            using (session.BeginTransaction())
            {

                var vproducts = session.CreateQuery("from Product p where p.ProductType='" + _productTypeCode + "' and p.Status='1'").Future<Product>();
                foreach (Product p in vproducts) {
                    productSamMDTO = new ProductSamMDTO();
                    productSamMDTO.ProductCode = p.ProductCode;
                    productSamMDTO.AC = p.ProductAc;
                    productSamMDTO.AR = p.ProductAr;
                    productSamMDTO.GAB = p.ProductGab;
                    productSamMDTO.CustomerCode = p.Customer.CustomerCode;
                    productSamMDTOs.Add(productSamMDTO);
                
                }

            }
            return productSamMDTOs;
        }
        // u14
        public List<hbse.DTO.ProductDTO> getProductsByCodeT10(string _productCode, ISession session)
        {

            List<ProductDTO> productDTOs = new List<ProductDTO>();
            ProductDTO productDTO = null;

            using (session.BeginTransaction())
            {

                var products = session.CreateQuery("from Product p where p.ProductCode like '%" + _productCode + "%' p.ProductType in(0,1) and p.Status='1'").Future<Product>();
                foreach (Product p in products)
                {
                    productDTO = new ProductDTO();
                    productDTO.Id = p.Id;
                    productDTO.ProductCode = p.ProductCode;
                    productDTO.CustomerCode = p.Customer.CustomerCode;
                    productDTOs.Add(productDTO);
                }

            }

            return productDTOs;
        }
        // u15
        public List<hbse.DTO.CustomerDTO> getCustomersByCode(string _customerCode,ISession session)
        {

            List<CustomerDTO> customerDTOs = new List<CustomerDTO>();
            CustomerDTO customerDTO = null;

            using (session.BeginTransaction())
            {

                var customers = session.CreateQuery("from Customer c where c.CustomerCode like '%"+_customerCode+"%'").Future<Customer>();
                foreach (Customer c in customers)
                {
                    customerDTO = new CustomerDTO();
                    customerDTO.Id = c.Id;
                    customerDTO.customerCode = c.CustomerCode;
                    customerDTO.customerName = c.CustomerName;
                    customerDTOs.Add(customerDTO);
                }

            }

            return customerDTOs;
        }
    }
}
